package com.zzh.mvvm.binding.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;

import com.zzh.lib.core.lang.HStrUtils;
import com.zzh.mvvm.binding.click.RxViews;
import com.zzh.mvvm.binding.command.BindingCommand;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * Created by ZZH on 2022/6/15.
 *
 * @Date: 2022/6/15
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description:
 */
public class ViewAdapter {
    //防重复点击间隔(秒)
    public static final int CLICK_INTERVAL = 1;

    /**
     * requireAll 是意思是是否需要绑定全部参数, false为否
     * View的onClick事件绑定
     * onClickCommand 绑定的命令,
     * isThrottleFirst 是否开启防止过快点击
     */
    @BindingAdapter(value = {"onClickCommand", "isThrottleFirst"}, requireAll = false)
    public static void onClickCommand(View view, final BindingCommand clickCommand, final boolean isThrottleFirst) {
        if (isThrottleFirst) {
            RxViews.clicks(view)
                    .subscribe((Consumer) object -> {
                        if (clickCommand != null) {
                            clickCommand.execute();
                        }
                    });
        } else {
            RxViews.clicks(view)
                    .throttleFirst(CLICK_INTERVAL, TimeUnit.SECONDS)//1秒钟内只允许点击1次
                    .subscribe((Consumer) object -> {
                        if (clickCommand != null) {
                            clickCommand.execute();
                        }
                    });
        }
    }

    /**
     * view的onLongClick事件绑定
     */
    @BindingAdapter(value = {"onLongClickCommand"}, requireAll = false)
    public static void onLongClickCommand(View view, final BindingCommand clickCommand) {
        RxViews.longClicks(view)
                .subscribe((Consumer) object -> {
                    if (clickCommand != null) {
                        clickCommand.execute();
                    }
                });
    }

    /**
     * 回调控件本身
     *
     * @param currentView
     * @param bindingCommand
     */
    @BindingAdapter(value = {"currentView"}, requireAll = false)
    public static void replyCurrentView(View currentView, BindingCommand bindingCommand) {
        if (bindingCommand != null) {
            bindingCommand.execute(currentView);
        }
    }

    /**
     * view是否需要获取焦点
     */
    @BindingAdapter({"requestFocus"})
    public static void requestFocusCommand(View view, final Boolean needRequestFocus) {
        if (needRequestFocus) {
            view.setFocusableInTouchMode(true);
            view.requestFocus();
        } else {
            view.clearFocus();
        }
    }

    /**
     * view的焦点发生变化的事件绑定
     */
    @BindingAdapter({"onFocusChangeCommand"})
    public static void onFocusChangeCommand(View view, final BindingCommand<Boolean> onFocusChangeCommand) {
        view.setOnFocusChangeListener((v, hasFocus) -> {
            if (onFocusChangeCommand != null) {
                onFocusChangeCommand.execute(hasFocus);
            }
        });
    }

    /**
     * view的显示和占位隐藏
     */
    @BindingAdapter(value = {"isVisible"}, requireAll = false)
    public static void isVisible(View view, final Boolean visibility) {
        if (visibility) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * view的显示隐藏
     */
    @BindingAdapter(value = {"isGone"}, requireAll = false)
    public static void isGone(View view, final Boolean visibility) {
        if (visibility) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    /**
     * EditText 输入改变事件
     *
     * @param view               输入框
     * @param afterTextChanged   输入框字符改变之前回调
     * @param beforeTextChanged  输入框字符改变之后回调
     * @param onInputTextChanged 输入框字符改变回调
     */
    @BindingAdapter(value = {"afterTextChanged", "beforeTextChanged", "onInputTextChanged"}, requireAll = false)
    public static void onTextChangedCommand(EditText view, final BindingCommand afterTextChanged, final BindingCommand onInputTextChanged, final BindingCommand beforeTextChanged) {
        view.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (beforeTextChanged != null) {
                    Object[] obj = new Object[4];
                    obj[0] = s;
                    obj[1] = start;
                    obj[2] = count;
                    obj[3] = after;
                    beforeTextChanged.execute(obj);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (onInputTextChanged != null) {
                    Object[] obj = new Object[4];
                    obj[0] = s;
                    obj[1] = start;
                    obj[2] = before;
                    obj[3] = count;
                    onInputTextChanged.execute(obj);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (afterTextChanged != null) {
                    afterTextChanged.execute(s.toString());
                }
            }
        });
    }


    /**
     * 设置TextView的文字，如果为空则隐藏
     *
     * @param view TextView
     * @param cs   文字
     */
    @BindingAdapter(value = {"setTextAndEmptyGone"}, requireAll = false)
    public static void setTextAndEmptyGone(TextView view, CharSequence cs) {

        if (HStrUtils.isEmpty(cs)) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
            view.setText(cs);
        }
    }
}
